/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.modules.biz.domain.EntranceGuard;
import me.zhengjie.modules.biz.service.EntranceGuardService;
import me.zhengjie.modules.biz.service.dto.EntranceGuardQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-18
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "entrance-guard管理")
@RequestMapping("/api/entranceGuard")
public class EntranceGuardController {

    private final EntranceGuardService entranceGuardService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('entranceGuard:list')")
    public void exportEntranceGuard(HttpServletResponse response, EntranceGuardQueryCriteria criteria) throws IOException {
        entranceGuardService.download(entranceGuardService.queryAll(criteria), response);
    }


    @GetMapping()
    @Log("查询entrance-guard")
    @ApiOperation("查询entrance-guard")
    public ResponseEntity<Object> queryEntranceGuard(EntranceGuardQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(entranceGuardService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增entrance-guard")
    @ApiOperation("新增entrance-guard")
//    @PreAuthorize("@el.check('entranceGuard:add')")
    public ResponseEntity<Object> createEntranceGuard(@Validated @RequestBody EntranceGuard resources){
        return new ResponseEntity<>(entranceGuardService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改entrance-guard")
    @ApiOperation("修改entrance-guard")
//    @PreAuthorize("@el.check('entranceGuard:edit')")
    public ResponseEntity<Object> updateEntranceGuard(@Validated @RequestBody EntranceGuard resources){
        entranceGuardService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除entrance-guard")
    @ApiOperation("删除entrance-guard")
//    @PreAuthorize("@el.check('entranceGuard:del')")
    public ResponseEntity<Object> deleteEntranceGuard(@RequestBody Integer[] ids) {
        entranceGuardService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/3d/entrance_guard")
    @Log("查询entrance-guard")
    @ApiOperation("查询entrance-guard")
    public ResponseEntity<Object> queryEntranceGuardFor3d(EntranceGuardQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(entranceGuardService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/add-list")
    //
    public ResponseEntity<Object> addList(){
        entranceGuardService.addList();
        return new ResponseEntity<>(HttpStatus.OK);
    }



    @AnonymousAccess
    @GetMapping("/list")
    @Log("门禁列表")
//    @Scheduled(cron = "0 0 12 * * ?")
    //  /api/entranceGuard/list
    public void list(){
        entranceGuardService.list();
//        String name = data.get("name");
    }

//    @AnonymousAccess
//    @PostMapping("/doControl")
//    @Log("门禁控制")
//    //  /api/entranceGuard/doControl
//    public ResponseEntity<Object>doControl(@RequestBody Map<String,Integer> data){
//        Integer id = data.get("id");
//        Integer state = data.get("state");
//        entranceGuardService.doControl(id,state);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }
    @AnonymousAccess
    @PostMapping("/states")
    @Log("门禁状态接口")
    public ResponseEntity<Object>getStates(){
        entranceGuardService.states();
        return new ResponseEntity<>(HttpStatus.OK);

    }

    @GetMapping("/3d/listFloor")
    @AnonymousAccess
    public ResponseEntity<Object> listFloor(Integer floor){
        return new ResponseEntity<>(entranceGuardService.listFloor(floor),HttpStatus.OK);
    }

    @GetMapping("/3d/getCountByFloor")
    @AnonymousAccess
    public ResponseEntity<Object> getCountByFloor(){
        return new ResponseEntity<>(entranceGuardService.getCountByFloor(),HttpStatus.OK);
    }

    @AnonymousPostMapping("/control")
    @Log("门禁开关门控制")
    @ApiOperation("门禁开关门控制")
    public ResponseEntity<Object> doControl(@RequestParam Integer id, @RequestParam Integer state){
        entranceGuardService.control(id, state);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
